<?php
/**
 * User: xuanbang.wen
 * Date: 2019/8/17
 */

namespace App\Web\Controller;

use App\Web\Facades\UserAuth;
use App\Web\Model\Coin as CoinModel;
use App\Web\Service\RightCheck;
use EchoPHP\Facades\Db;
use EchoPHP\Http\Request;
use App\Web\Model\User as UserModel;
use EchoPHP\Http\Response\JsonResponse;
use Exception;

class Coin extends Controller {

    protected $rightCheck = [
        'index' => RightCheck::RIGHT_DOWNLOADCOUNT_ALL,
        'records' => RightCheck::RIGHT_DOWNLOADCOUNT_ALL,
        'modifyCount' => RightCheck::RIGHT_DOWNLOADCOUNT_ALL,
        'getRemainCoin' => RightCheck::RIGHT_DOWNLOADCOUNT_ALL,
    ];

    /**
     * 获取用户列表、查看其下载数明细
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function index(Request $request) {
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);
        $oprRole = UserAuth::getLoginInfo('issuper', '');
        $teamCode = substr(UserAuth::getLoginInfo('teamcode', ''), 0, 5);
        $search = [
            'cname' => $request->getParam('cname', ''),
            'userid' => $request->getParam('userid'),
            'username' => $request->getParam('username'),
            'area' => $request->getParam('area'),
            'teamcode' => $request->getParam('teamcode'),
            'withchild' => $request->getParam('withchild', 0),
            'teamrole' => $request->getParam('teamrole'),
        ];

        $userModel = new UserModel();
        list($pageNum, $rowFrom, $rowTo) = $userModel->paginationParameters($pageNum, $pageLimit);
        $field = 'userid,username,cname,downloadcount,islock,area,User.teamcode,User.teamrole,User.teamid,Team.teamname';
        $userList = $userModel->search($field, $search, $rowFrom, $rowTo, null, $oprRole == 1 ? null : $teamCode, false, true);

        $count = $userModel->getListCount($search, $oprRole == 1 ? null : $teamCode);

        $resultBody = [
            'status' => true,
            'message' => 'success',
            'totalCount' => intval($count),
            'pagenum' => $pageNum,
            'userlist' => $userList,
        ];
        return $this->json($resultBody);
    }

    /**
     * 获取用户剩余下载数
     * @param Request $request
     * @return array|mixed
     * @throws Exception
     */
    public function getRemainCoin(Request $request) {
        $userId = $request->getParam('userid/d', 0);
        $userModel = new UserModel();
        $userInfo = $userModel->getInfo($userId, 'downloadcount,approver');
        return $this->json(['status' => true, 'message' => '获取成功', 'userinfo' => $userInfo]);
    }

    /**
     * 查询下载数增减明细
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function records(Request $request) {
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);
        $oprRole = UserAuth::getLoginInfo('issuper', '');
        $teamCode = substr(UserAuth::getLoginInfo('teamcode', ''), 0, 5);
        $search = [
            'username' => $request->getPost('userid'),
            'type' => $request->getPost('type'),
            'oprname' => $request->getPost('oprname'),
            'datestart' => $request->getPost('datestart'),
            'dateend' => $request->getPost('dateend'),
        ];
        $coinModel = new CoinModel();

        list($pageNum, $rowFrom, $rowTo) = $coinModel->paginationParameters($pageNum, $pageLimit);
        $count = $coinModel->getListCount($search, $oprRole == 1 ? null : $teamCode);
        $records = $coinModel->search('', $search, $rowFrom, $rowTo, '', $oprRole == 1 ? null : $teamCode);
        $resultBody = [
            'status' => true,
            'message' => 'success',
            'totalCount' => intval($count),
            'pagenum' => $pageNum,
            'records' => $records,
        ];
        return $this->json($resultBody);
    }

    /**
     * 用户下载数修改
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function modifyCount(Request $request) {
        $oprId = $this->getLoginId();
        $oprName = UserAuth::getLoginCname();

        $userId = $request->getPost('userid/d');
        $type = $request->getPost('type');
        $coinCount = $request->getPost('count');
        $approver = $request->getPost('approver');
        $memo = $request->getPost('memo');

        $coinModel = new CoinModel();
        $result = $coinModel->sysChangeCoin($oprId, $oprName, $userId, $type, $coinCount, $approver, $memo);
        return $this->json($result);
    }

    /**
     * 下载数报表
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function coinReport(Request $request) {
        $oprId = $this->getLoginId();
        $oprName = UserAuth::getLoginCname();
        $oprRole = UserAuth::getLoginInfo('issuper', '');
        if ($oprRole == 1) {
            $teamCode = $request->getParam('teamcode');
        } else {
            $teamCode = substr(UserAuth::getLoginInfo('teamcode', ''), 0, 5);
        }
        $search = [
            'teamcode' => $request->getParam('teamcode'),
        ];
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);
        $startDate = $request->getParam('datestart');
        $endDate = $request->getParam('dateend');

        $coinModel = new CoinModel();
        list($pageNum, $rowFrom, $rowTo) = $coinModel->paginationParameters($pageNum, $pageLimit);
        $count = $coinModel->getCoinReportCount($teamCode, $startDate, $endDate, $search);
        $result = $coinModel->getCoinReport($teamCode, $startDate, $endDate, $rowFrom, $rowTo, $search);

        $resultBody = [
            'status' => true,
            'message' => 'success',
            'totalCount' => intval($count[0]['count']),
            'pagenum' => $pageNum,
            'memberList' => $result,
        ];

        return $this->json($resultBody);
    }

    /**
     * 下载数报表
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function coinReportTeam(Request $request) {
        $oprId = $this->getLoginId();
        $oprName = UserAuth::getLoginCname();
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);
        $userID = $request->getParam('userid');
        $teamCode = $request->getParam('teamcode');
        $startDate = $request->getParam('datestart');
        $endDate = $request->getParam('dateend');

        if (empty($teamCode)) {
            $userModel = new UserModel();
            $oprInfo = $userModel->getInfoWithTeam($oprId);
            $teamCode = $oprInfo['teamcode'];
        }

        $coinModel = new CoinModel();
        list($pageNum, $rowFrom, $rowTo) = $coinModel->paginationParameters($pageNum, $pageLimit);
        $count = $coinModel->getCoinReportCountTeam($teamCode, $userID, $startDate, $endDate);
        $result = $coinModel->getCoinReportTeam($teamCode, $userID, $startDate, $endDate, $rowFrom, $rowTo);


        $resultBody = [
            'status' => true,
            'message' => 'success',
            'totalCount' => intval($count[0]['count']),
            'pagenum' => $pageNum,
            'memberList' => $result,
        ];

        return $this->json($resultBody);
    }


}